<template>
	<view class="login_content">
		<uni-icons type="contact-filled" size="100"></uni-icons>
		<button  open-type="getUserInfo" class="login_content_login" @click="getUserInfo">
		一键登录
		</button >
		<view class="login_content_login_tip">
			登录后享受更多权益
		</view>
	</view>
</template>

<script>
	import {mapState,mapMutations} from "vuex"
	
	export default {
		name:"my-login",
		data() {
			return {
				
			};
		},
		computed:{
			...mapState("m_user",["token"]),
			...mapState("m_user",["userInfo","rederictInfo"]),
		},
		methods:{
			//获取用户的基本信息
			...mapMutations("m_user",["updateuserInfo"]),
			...mapMutations("m_user",["updateToken","updateRederictInfo"]),
			getUserInfo(){
				uni.getUserProfile({
				    desc: '123',
				     lang: 'zh_CN',
				    success: (res)=> {    
				       
					   this.updateuserInfo(res);
					   console.log(res);
					   this.getToken(this.userInfo);
					 
				    },
					fail:(err)=>{ 
						return uni.$showMsg("您取消了授权");
					}
				})    
			},
			async getToken(query){
				 const [err,res] = await uni.login().catch(err=>err);
				 console.log(res);
				 if(err) return uni.$showMsg("登录失败!");
				 if(res.errMsg == "login:ok"){
						console.log(res.code);
						const queryObj={
							code:res.code,
							encryptedData:query.encryptedData,
							iv:query.iv,
							rawData:query.rawData,
							signature:query.signature
						}
						//换取token
						const {data:result} = await uni.$http.post("/api/public/v1/users/wxlogin",queryObj);
						console.log(result,queryObj);
						if(JSON.stringify(this.rederictInfo) != '{}' && this.rederictInfo['opentype'] == "switchTab"){	
							uni.switchTab({
								url:this.rederictInfo['url'],
								complete:()=>{
									this.updateRederictInfo({})
								}
							})
							
							
						};
						// if(result.meta.status != "200" ) return uni.$showMsg("请求失败!");
						setTimeout(
							function(){
								uni.$showMsg("登录成功!");
							},1000
						)
						const token = "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEyLCJpYXQiOjE1MjU0MDIyMjMsImV4cCI6MTUyNTQ4ODYyM30.g-4GtEQNPwT_Xs0Pq7Lrco_9DfHQQsBiOKZerkO-O-o";
						this.updateToken(token);
				 }
				
			}
		}
		
	}
</script>

<style lang="scss" scoped>
	.login_content{
		width: 100%;
		height: 100vh;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
		background: -webkit-linear-gradient(top, #dddddd 0%,#ffffff 100%);
	}
	.login_content_login{
		width: 80%;
		background-color: #ba0000;
		line-height:50px;
		border-radius:30px;
		margin-top:20px;
		color:#fff;
		text-shadow: 2px 2px  2px #222 ;
		text-align: center;
	}
	.login_content_login_tip{
		font-size: 14px;
		color: gray;
		margin-top: 10px;
	}
</style>
